﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace WebApplication2.Account
{
    public partial class Login : Page
    {
        String username;
        String password;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                if (Request.IsAuthenticated && !string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))

                    Response.Redirect("~/Unauthorized.aspx");
            }

        }
        private bool authenticate(String username, String password)
        {
            if (Membership.ValidateUser(username, password))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

       protected void login_Click(object sender, EventArgs e)
       {
           username = LoginForm.UserName;
           password = LoginForm.Password;
           if (Membership.ValidateUser(username, password))
           {
              /* if (Roles.IsUserInRole(username, "Admin"))
               {
                   Response.Redirect("~/Admin/Default.aspx");
               }

               if (Roles.IsUserInRole(username, "Schrijf"))
               {

               }

               if (Roles.IsUserInRole(username, "Lees"))
               {

               }*/
           }
       }

       protected void LoginForm_Authenticate(object sender, AuthenticateEventArgs e)
        {
            bool Authenticated = false;
            Authenticated = authenticate(LoginForm.UserName, LoginForm.Password);
            e.Authenticated = Authenticated;  
        }

        protected void LoginForm_LoggedIn(object sender, EventArgs e)
        {
           if (Roles.IsUserInRole(username, "Admin"))
            {
                Response.Redirect("~/Admin.aspx");
            }

            if (Roles.IsUserInRole(username, "Schrijf"))
            {
                Response.Redirect("~/Edit.aspx");
            }

            if (Roles.IsUserInRole(username, "Lees"))
            {
                Response.Redirect("~/");
            }
        }
    }
}